// pages/xgphone/xgphone.js
import store from "../../store/index";
import create from "../../utils1/create";

create.Page(store, {
  use: ['isToken', "infouser", "globhttp"],

  /**
   * 页面的初始数据
   */
  data: {
    isHidden: true,
    show: false,
    phonemessage: "请输入新的手机号",
    title: '请阅读并同意使用协议及隐私政策',
    checked: false,
    tipsCode: "获取验证码",
    timeNum: 60,
    clikType: false,
    phone: '',
    phoneCode: "",
    buttonmesage: "确定绑定",
    timer: null
  },

  xgphone: function () {
    if (this.data.phone === '') {
      wx.showToast({
        icon: "none",
        title: '请输入手机号',
        duration: 2000
      });
    } else if (!/^1[3456789]\d{9}$/.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号格式',
        icon: 'none'
      });
      return;
    } else {
      console.log("infouser", store.data.infouser.Pson_Name);
      console.log("infouser", store.data.infouser);
      wx.request({
        url: this.store.data.globhttp + "UpdateUserInfo",
        method: 'POST',
        header: {
          'content-type': 'application/json',
          "content-type": "application/x-www-form-urlencoded"
        },
        data: JSON.stringify({
          User_Name: store.data.infouser.User_Name,
          User_Id: store.data.infouser.User_Id,
          Token_Code: store.data.isToken,
          User_Tel: this.data.phone,
        }),
        success: (res) => {
          console.log("res", res);
          if (res.data.RltCode === 13) {
            this.store.setToken("setphone");
          } else if (res.data.RltCode === 1) {
          setTimeout(function() {
            wx.showToast({
              icon: "none",
              title: '修改手机号成功',
              duration: 1000
            });
          },2000)
          wx.showToast({ icon: 'loading', title: '', mask: true });
            store.data.xgphone = this.data.phone;
            store.data.infouser.Pson_Tel = this.data.phone;
            wx.setStorageSync('infouser', store.data.infouser);
           setTimeout(function() {
            wx.switchTab({
              url: '/pages/mine/mine'
            });
           },3000)
          } else if (res.data.RltCode === 0) {
            console.log(res.data.RltMsg);
            wx.showToast({
              icon: "none",
              title: "修改手机号失败",
              duration: 2000
            });
          } else {
            console.log(res.data.RltMsg);
            wx.showToast({
              icon: "none",
              title: "未知错误",
              duration: 2000
            });
          }
        },
        fail: (error) => {
          console.error('请求失败:', error);
          wx.showToast({
            icon: "none",
            title: "网络请求失败",
            duration: 2000
          });
        }
      });
    }
  },

  inputChange: function (e) {
    console.log(e);
    this.setData({
      phone: e.detail.value
    });
  },

  bindKeyInput: function (e) {
    const { inputname } = e.currentTarget.dataset;
    this.setData({
      [inputname]: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    setTimeout(() => {
      this.setData({
        isHidden: false
      });
    }, 500); // 500毫秒后显示页面
    console.log("绑定手机号传值", options);
    if (options.id === "0") {
      this.setData({
        buttonmesage: "确定绑定",
        phonemessage: "请输入您需要绑定的手机号"
      });
    } else if (options.id === '1') {
      this.setData({
        buttonmesage: "确定修改",
        phonemessage: "请输入您新的手机号"
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (this.data.timer) {
      clearInterval(this.data.timer);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
});